//
//  a.m
//  ClockIn_v2
//
//  Created by Tomy on 13/11/2020.
//

#import "WatchViewController.h"
#import "Masonry.h"
#import "Utility.h"
#import "CollectionCell.h"

@interface WatchViewController () {
    NSString *date;
    NSString *place;
    NSString *attraction;
    NSString *experience;
    NSInteger numberPics;
}

@end

@implementation WatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 4个标签栏
    UILabel *lastView = nil;
    NSArray *clockInList = @[date, place, attraction, experience];
    for (NSString *str in clockInList) {
        UILabel *label = [UILabel new];
        label.text = str;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.numberOfLines = 0;
        [self.view addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.85 * self.view.bounds.size.width);
            if (lastView == nil) {
                make.top.equalTo(self.view).offset([Utility getTopBarWidth] + 45);
            }
            else {
                make.top.equalTo(lastView.mas_bottom).offset(20);
            }
            make.centerX.equalTo(self.view.mas_centerX);
        }];
        
        lastView = label;
    }
    
    // 图片
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemEdge = 90;
    layout.itemSize = CGSizeMake(itemEdge, itemEdge);
    // 同一行中各item最小间距
    layout.minimumInteritemSpacing = 10;
    // 各行之间最小间距
    layout.minimumLineSpacing = 10;
    
    layout.sectionInset = UIEdgeInsetsMake(5, 5 ,5, 5);
    layout.sectionFootersPinToVisibleBounds = YES;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    // 显示图片
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    collection.showsVerticalScrollIndicator = NO;
    collection.scrollEnabled = YES;
    collection.delegate = self;
    collection.dataSource = self;
    
    // 注册cell
    [collection registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"collectioncell"];
    collection.backgroundColor = [UIColor clearColor];
    [self.view addSubview:collection];
    [collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(20);
        make.bottom.equalTo(self.view).offset(-self.tabBarController.tabBar.frame.size.height - 20);
        make.width.mas_equalTo(0.85 * self.view.bounds.size.width);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
}

- (id)initWithDate:(NSString *)_date place:(NSString *)_place attraction:(NSString *)_attraction experience:(NSString *)_experience{
    self = [super init];
    if(self){
        date = _date;
        place = _place;
        attraction = _attraction;
        experience = _experience;
        numberPics = 5;
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return numberPics;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"collectioncell";
    CollectionCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CollectionCell alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    }
    
    //NSString *hint = [NSString stringWithFormat:@"图片 %zd", indexPath.item];
    NSString *hint = [NSString stringWithFormat:@"图片"];
    
    [cell setupWithHint:hint];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

@end

